<%@ include file="shared_header.jsp" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="vrp.SOrder" %>
<%@ page import="vrp.Customer" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
%>

<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
		<p>Hello, <%= user.getNickname() %>! (You can
		<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
		<p>Hello!
		<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
		to include your name with greetings you post.</p>
<%
    }
%>

		<h2>List of Sale Orders</h2>
		<table>
<%
	String querySOrders = "select from " + SOrder.class.getName();
    List<SOrder> sorders = (List<SOrder>) pm.newQuery(querySOrders).execute();
	String queryCustomers = "select from " + Customer.class.getName();
    List<Customer> customers = (List<Customer>) pm.newQuery(queryCustomers).execute();
    if (sorders.isEmpty()) {
%>
			<tr><td>There is no sale order in the database.</td></tr>
<%
    } else {
%>
			<tr>
				<th>Customer</th>
				<th>Date</th>
				<th>Recurring Type</th>
				<th>Start Date</th>
				<th>End Date</th>
				<th>Status</th>
				<th>Action</th>
			</tr>
<%
        for (SOrder o : sorders) {
%>
			<tr>
				<td><%= (pm.getObjectById(Customer.class, o.getCustomer())).getName() %></td>
				<td><%= sdf.format(o.getDate()) %></td>
				<td><%= o.getRecurringType() %></td>
				<td><%= sdf.format(o.getStartDate()) %></td>
				<td><%= sdf.format(o.getEndDate()) %></td>
				<td><%= o.getSOrderStatus() %></td>
				<td>
					<form action="/sorderitems.jsp" method="get" target="_blank">
						<input type="hidden" name="sorder" value="<%= o.getKey().getId() %>"/>
						<input type="submit" value="View Items"/>
					</form>
					<input type="button" value="Edit" class="buttonedit"/>
					<input type="button" value="Delete" class="buttondelete"/>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="7">
					<form action="/editsorder" method="post">
						<div><b>Edit Sale Order</b></div>
			    		<div>Customer:&nbsp;
			    			<select name="customer">
<%
        					for (Customer c : customers) {
%>			    			
							 	<option value="<%= c.getKey().getId() %>" <%= (c.getKey().getId()==o.getCustomer().getId())?"selected":"" %>><%= c.getName() %></option>
<%
        					}
%>							</select>
			    		</div>
			    		<div>Date:&nbsp;<input type="text" name="date" value="<%= sdf.format(o.getDate()) %>" /></div>
			    		<div>Recurring Type:&nbsp;<input type="text" name="recurringType" value="<%= o.getRecurringType() %>" /></div>
			    		<div>Start Date:&nbsp;<input type="text" name="startDate" value="<%= sdf.format(o.getStartDate()) %>" /></div>
			    		<div>End Date:&nbsp;<input type="text" name="endDate" value="<%= sdf.format(o.getEndDate()) %>" /></div>
			    		<div>SOrder Status:&nbsp;<input type="text" name="sorderStatus" value="<%= o.getSOrderStatus() %>" /></div>
						<input type="hidden" name="sorder" value="<%= o.getKey().getId() %>"/>
						<input type="submit" value="Edit" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="7">
					<form action="/deletesorder" method="post">
						<div><b>Delete Sale Order</b></div>
			    		<div>This action would delete the above sale order permanently. Proceed?</div>
						<input type="hidden" name="sorder" value="<%= o.getKey().getId() %>"/>
						<input type="submit" value="Delete" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
<%
        }
    }
%>
			
		</table>
		<input type="button" value="Add SOrder" class="buttonadd" />
  		<div style="display: none;">
			<form action="/addsorder" method="post">
	    		<div>Customer:&nbsp;
	    			<select name="customer">
<%
     				for (Customer c : customers) {
%>			    			
					 	<option value="<%= c.getKey().getId() %>"><%= c.getName() %></option>
<%
        			}
%>							
					</select>
	    		</div>
	    		<div>Recurring Type:&nbsp;<input type="text" name="recurringType" /></div>
	    		<div>Start Date:&nbsp;<input type="text" name="startDate" /></div>
	    		<div>End Date:&nbsp;<input type="text" name="endDate" /></div>
	    		<div><input type="submit" value="Add" /><input type="button" value="Cancel" class="buttoncancel" /></div>
	  		</form>
	  	</div>

<%
    pm.close();
%>

<%@ include file="shared_footer.jsp" %>